<?php
/**
 * 
 * @author huangjian E-mail:870596179@qq.com
 * @version 创建时间：2016年10月21日 上午10:42:24 
 * 类说明
 */
class AttributeAction extends BaseAction{
	
	/**
	 * @authority 浏览
	 */
	public function index(){
		$this->display('index');
	}
	
	//列表
	public function getAllData(){
		$Model = M($this->getActionName());
		$map = $this->loadSeachCondition();
		$map['flag'] = 1;
		$total = $Model->where($map)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$data = $Model->where($map)->limit($page->firstRow,$page->listRows)->order($page->sort)->select();
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	/**
	 * 组装条件
	 * @return multitype:multitype:string
	 */
	public function loadSeachCondition(){
		$map = array();
		/*
		if(!empty($_GET['field'])){
			$map['field'] = array("like","%".$_GET['field']."%");
		}
		*/
		return $map;
	}
	
	// 新增
	public function add(){
		$ProvinceModel = M('ZhProvince');$CityModel = M('ZhCity');$AreaModel = M('ZhArea');
		$province = $ProvinceModel->select();
		$city = $CityModel->where("province_id=".$province[0][id])->select();
		$area = $AreaModel->where("city_id=".$city[0][id])->select();
		$this->assign("province",$province);
		$this->assign("city",$city);
		$this->assign("area",$area);
		$this->display();
	}

	public function insert(){
		$Model = M($this->getActionName());
		unset ( $_POST [$Model->getPk()] );
		if (false === $Model->create()) {
			$this->error($Model->getError());
		}
		$data = $Model->create();
		$map['flag'] = 1;
		$max_seq = $Model->where($map)->order('seq desc')->getField('seq');
		$data['seq'] = $max_seq + 1;
		//保存当前数据对象
		if ($result = $Model->add($data)) { //保存成功
			$this->deleteSession();
			$this->ajaxReturn(1,L('ADMIN_ADD_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_ADD_ERROR'),0);
		}
	}
	
	
	
	//修改
	public function edit() {
		$Model = M($this->getActionName());
		$map['id'] = $_REQUEST['id'];
		$vo = $Model->where($map)->find();
		$this->assign('vo',$vo);
		$this->display();
	}
	
	/**
	 * 更新
	 */
	public function update() {
		$Model = D($this->getActionName());
		if(false === $Model->create()) {
			$this->error($Model->getError());
		}
		// 更新数据
		if(false !== $Model->save()) {
			$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
		}
	}
	
	/**
	 * 逻辑删除
	 */
	public function logicDel(){
		$Model = M($this->getActionName());
		$AddressModel = M("Address");
		if (!empty($Model)) {
			$pk = $Model->getPk();
			$id = $_REQUEST[$pk];
			if (isset($id)) {
				$map['flag'] = 1;
				$map['school_id'] = $id;
				$count = $AddressModel->where($map)->count();
				if($count != 0){
					$this->ajaxReturn(0,"删除失败！该学校已有用户绑定！",0);
				}
				$condition = array($pk => array('in', explode(',', $id)));
				if (false !== $Model->where($condition)->setField('flag',2)) {
					$this->deleteSession();
					$this->ajaxReturn(1,L('ADMIN_DELETE_SUCCESS'),1);
				} else {
					$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
				}
			} else {
				$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
			}
		}
	}
	
	//检查学校名是否可注册
	public function checkName(){
		$param = $_REQUEST['param'];
		$user = M($this->getActionName());
		$where['name'] = $param;
		$where['flag'] = 1;
		$count=$user->where($where)->count();
		if($count){
			echo '学校名已被注册！';
		}else{
			echo 'y';
		}
	}
	
	//检查学校名是否可注册，排除当前
	public function exCheckName(){
		$param = $_REQUEST['param'];
		$user = M($this->getActionName());
		if(!empty($_REQUEST['id'])){
			$map['id'] = $_REQUEST['id'];
			$result = $user->where($map)->find();
			if($param == $result['name']) {
				echo 'y';
				return;
			}else{
				$where['name'] = $param;
				$where['flag'] = 1;
			}
		}else{
			$where['name'] = $param;
			$where['flag'] = 1;
		}
		$count=$user->where($where)->count();
		if($count){
			echo '学校名已被注册！';
		}else{
			echo 'y';
		}
	}
}